package com.example.socdok;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class IntroState implements IState {

	Bitmap start;
	Bitmap start1;
	Bitmap blank;
	Bitmap config;
	BitmapDrawable startbtn;
	BitmapDrawable blankbtn;
	Bitmap title_I;
	Bitmap back_g[];
	Bitmap configBtn;
	Bitmap op;
	static boolean clickStart;
	static boolean clickConfig;
	Canvas newCanvas;
	SurfaceHolder stateholder;
	int i=0,alpha=0;
	int x;
	int y;
	@Override
	public void Init(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		stateholder = holder;
		startbtn = AppManager.getInstance().getBitmapDrawable(R.drawable.start);
		blankbtn = AppManager.getInstance().getBitmapDrawable(R.drawable.blank);
		start1 = startbtn.getBitmap();
		blank = blankbtn.getBitmap();
		title_I = AppManager.getInstance().getBitmap(R.drawable.title);
		back_g = new Bitmap[2];
		back_g[0] = AppManager.getInstance().getBitmap(R.drawable.background1);
		back_g[1] = AppManager.getInstance().getBitmap(R.drawable.book);
		start = start1;
		configBtn = AppManager.getInstance().getBitmap(R.drawable.configbtn);
		config = configBtn;
		clickStart = false;
		clickConfig = false;
		
	}

	@Override
	public void Destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Update() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Render(Canvas canvas) {
		
	//	back.satrtAnimaiton(flowA);
		// TODO Auto-generated method stub
		y = canvas.getHeight();
		x = canvas.getWidth();
		
		if(clickConfig){
			canvas.drawBitmap(back_g[0],0 - i,0, null);
			canvas.drawBitmap(back_g[1],back_g[0].getWidth() - i,0, null);
		}

		else if(clickStart && i<=back_g[0].getWidth()){
			canvas.drawBitmap(back_g[0],0 - i,0, null);
			canvas.drawBitmap(back_g[1],back_g[0].getWidth() - i,0, null);
			i = i + 10;
			if( i > back_g[0].getWidth())AppManager.getInstance().getGameView().ChangeGameState(new PlayGameState());
		}
		else{
			canvas.drawBitmap(back_g[0],0 - i,0, null);
			canvas.drawBitmap(start, (x/5)*2, y/2, null);
			canvas.drawBitmap(title_I, 10, 10, null);
			canvas.drawBitmap(config, (x/5)*2, y/3*2, null);
		}
		
		//paint.setAlpha(alpha);
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		AppManager.getInstance().getGameView().ChangeGameState(new CreditState());
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();
		
		int m_x, m_y;
		m_x = (int) event.getX();
		m_y = (int) event.getY();
		
		Rect rt = new Rect();
		
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			rt.set((x/5)*2, y/2, (x/5)*2 + start.getWidth(), y/2 + start.getHeight());
			if(rt.contains(m_x, m_y)){
				clickStart = true;
			}
		
			rt.set((x/5)*2, y/3*2, (x/5)*3 + configBtn.getWidth(), y/3*2 + configBtn.getHeight());
			if(rt.contains(m_x, m_y)){
				clickConfig = true;
				AppManager.getInstance().getGameView().ChangeGameState(new configState());
			}
			
		}
		return true;
	}

	@Override
	public void Init() {
		// TODO Auto-generated method stub
		
	}

}